home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Programming / AmigaTalk / general / Pen.st < prev    next >
Text File  |  2000-04-28  |  6KB  |  209 lines

  1. "-------------------------------------------------------------"
  2. "  the following use the primitives interfacing to the plot(3)"
  3. "  routines.                                                  "
  4. "           pen - a simple drawing instrument                 "
  5. "-------------------------------------------------------------"
  6.  
  7. Class Pen :Object
  8. ! title x y angle width height fpen bpen !
  9. [
  10.    movePlotEnvBy: deltaPoint
  11.       <primitive 169 2 title (deltaPoint x) (deltaPoint y)>.
  12.       ^ self
  13. |
  14.    setLineType: bitPattern
  15.       <primitive 179 bitPattern>
  16. |
  17.    drawText: text at: aPoint
  18.       <primitive 178 text (aPoint x) (aPoint y) fpen bpen>.
  19.       x <- (aPoint x).
  20.       y <- (aPoint y)
  21. |
  22.    drawBox: fromPoint to: toPoint
  23.       <primitive 175 (fromPoint x) (fromPoint y) (toPoint x) (toPoint y)>.
  24.       x <- (toPoint x).
  25.       y <- (toPoint y)
  26. |
  27.    drawCircleAt: aPoint radius: r
  28.       <primitive 174 (aPoint x) (aPoint y) r>.
  29.       x <- (aPoint x).
  30.       y <- (aPoint y)        "Leave us at the center of the circle."
  31. |
  32.    circleRadius: rad         "Draw a circle centered at current location."
  33.       <primitive 174 x y rad>
  34. |
  35.    drawTo: aPoint
  36.       <primitive 172 (aPoint x) (aPoint y)>.
  37.       x <- (aPoint x).
  38.       y <- (aPoint y)
  39. |
  40.    goTo: aPoint
  41.       <primitive 171 (aPoint x) (aPoint y)>.
  42.       x <- aPoint x.
  43.       y <- aPoint y
  44. |
  45.    drawLine: fromPoint to: toPoint
  46.       <primitive 177 (fromPoint x) (fromPoint y) (toPoint x) (toPoint y)>.
  47.       x <- (toPoint x).
  48.       y <- (toPoint y)
  49. |
  50.    drawPoint: aPoint
  51.       <primitive 173 (aPoint x) (aPoint y)>.
  52.       x <- (aPoint x).
  53.       y <- (aPoint y)
  54. |
  55.    direction             "Which way are we going?" 
  56.       ^ angle
  57. |
  58.    direction: radians    "Set the direction to go."
  59.       angle <- radians
  60. |
  61.    erase                 "Blank out the Current PlotEnv Window."
  62.       <primitive 170>
  63. |
  64.    extent                "Tell User how large the Plot area is."
  65.       ^ width @ height
  66. |
  67.    location              "Tell User where the pen is."
  68.       ^ x @ y
  69. |
  70.    titleIs
  71.       ^ title
  72. |
  73.    center                "goTo the center of the Plot region."
  74.       self goTo: (width / 2) @ (height / 2)
  75. |
  76.    tellPens              "Tell User which pens are being used."
  77.       ^ fpen @ bpen
  78. |
  79.    setPens: penSet       "penSet is of Class Point (front @ back)."
  80.       <primitive 176 (penSet x) (penSet y)>.
  81.       fpen <- (penSet x).
  82.       bpen <- (penSet y)
  83. |
  84.    go: anAmount ! newx newy !
  85.       (angle isKindOf: Radian)
  86.         ifTrue:  [newx <- ((angle sin) * anAmount) rounded + x.
  87.                   newy <- ((angle cos) * anAmount) rounded + y
  88.                  ]
  89.         ifFalse: [newx <- (((angle radians) sin) * anAmount) rounded + x.
  90.                   newy <- (((angle radians) cos) * anAmount) rounded + y
  91.                  ].
  92.  
  93.       self drawTo: newx @ newy  "go: leaves drawn pixels as it moves."
  94. |
  95.    turn: addedRadians
  96.       angle <- angle + addedRadians
  97. |
  98.    new
  99.       title  <- 'Unknown Plot'. 
  100.       angle  <- Radian new: 0.
  101.       x      <- 160.
  102.       y      <- 100.
  103.       width  <- 320.
  104.       height <- 200.
  105.       fpen   <- 1.
  106.       bpen   <- 0.
  107.       ^ self
  108. |
  109.    new: newPlotTitle            "We want a unique Plot Title!"
  110.       (newPlotTitle isKindOf: String)
  111.          ifTrue:  [ title <- newPlotTitle ]
  112.          ifFalse: [ title <- 'Unknown Plot'].
  113.  
  114.       angle  <- Radian new: 0.
  115.       x      <- 160.
  116.       y      <- 100.
  117.       width  <- 320.
  118.       height <- 200.
  119.       fpen   <- 1.
  120.       bpen   <- 0.
  121.       ^ self
  122. |
  123.    openPlotEnv: sizePoint ! nx ny !
  124.       nx <- sizePoint x.
  125.       ny <- sizePoint y.
  126.        
  127.       (<primitive 169 1 title nx ny> == true)
  128.          ifFalse:  [ self error: 'openPlotEnv ', title, ' did NOT open!'. 
  129.                      ^ nil
  130.                    ]
  131.          ifTrue:   [ angle  <- Radian new: 0.
  132.                      x      <- (nx / 2).
  133.                      y      <- (ny / 2).
  134.                      width  <- nx.
  135.                      height <- ny.
  136.                      ^ self
  137.                    ]
  138. |
  139.    closePlotEnv: whichPlotTitle
  140.       (<primitive 169 0 whichPlotTitle> == true)
  141.         ifFalse: [self error: 'PlotEnv ',whichPlotTitle,' did NOT close!'.
  142.                   ^ self
  143.                  ].
  144.       ^ nil
  145. ]
  146.  
  147. "----------------------------------------------------"
  148. " FormPen - a collection of lines                    "
  149. " I don't think this class will work as written (JTS)"
  150. "----------------------------------------------------"
  151.  
  152. Class FormPen :Pen
  153. ! lines !
  154. [
  155.    new
  156.       lines <- Bag new
  157. |
  158.    add: startingPoint to: endingPoint "Methinks this is broken:"
  159.       lines add: ( Point new ; x: startingPoint ; y: endingPoint )
  160. |
  161.    with: aPen displayAt: location ! xOffset yOffset sPoint ePoint !
  162.       xOffset <- (location x).
  163.       yOffset <- (location y).
  164.  
  165.       lines do: [:pair |
  166.  
  167.          sPoint <- (pair x).
  168.          ePoint <- (pair y).
  169.  
  170.          aPen   goTo: (sPoint x + xOffset) @ (sPoint y + yOffset).
  171.          aPen drawTo: (ePoint x + xOffset) @ (ePoint y + yOffset).
  172.          ].
  173. ]
  174.  
  175. "----------------------------------------------------"
  176. " SavePen - a way to save the drawings made by a pen "
  177. "----------------------------------------------------"
  178.  
  179. Class SavePen :FormPen
  180. ! saveForm !
  181. [
  182.    setForm: aForm
  183.       saveForm <- aForm
  184. |
  185.    goTo: aPoint
  186.       super    goTo: aPoint.
  187.       saveForm  add: (self location) to: aPoint.
  188.       super    goTo: aPoint.
  189. ]
  190.  
  191. "-----------------------------------------------------"
  192. " ShowPen - show off some of the capabilities of pens."
  193. "-----------------------------------------------------"
  194.  
  195. Class ShowPen :Object
  196. ! bic !
  197. [
  198.    withPen: aPen "aPen has to be init'd & open before using these methods."
  199.       bic <- aPen
  200. |
  201.    poly: nSides length: length
  202.       nSides timesRepeat:  [ bic   go: length.
  203.                              bic turn: (6.2831853 / nSides)  "2 PI"
  204.                            ]
  205. |
  206.    spiral: n angle: a
  207.       (1 to: n) do: [:i | bic go: i. bic turn: a]
  208. ]
  209.